﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VirtualPress.Infrastructure.Entities.Repository;
using VirtualPress.Infrastructure.Entities.Enum;
using System.Data.SqlClient;
using System.Configuration;
using MySql.Data.MySqlClient;
using System.Data;

namespace VirtualPress.Infrastructure.Entities.Entity
{
    public class StreamFactory : IAccessible<StreamNo, StreamFactory>, ILogicEntity<StreamNo, StreamFactory>
    {
        #region Fileds
        public int Id { get; set; }
        /// <summary>
        /// 1：学术期刊  2：非学术期刊（杂志）
        /// </summary>
        public BuildStreamType StreamFor { get; set; }
        /// <summary>
        /// 关联Id
        /// </summary>
        public int RelationId { get; set; }
        /// <summary>
        /// 标示，通常为yyMM
        /// </summary>
        public string Seed { get; set; }
        /// <summary>
        /// 序号
        /// </summary>
        public int No { get; set; }
        #endregion

        protected override void Covariant(StreamNo model)
        {
            if (model != null)
            {
                this.Id = model.Id;
                this.No = model.No;
                this.RelationId = model.RelationId;
                this.Seed = model.Seed;
                this.StreamFor = (BuildStreamType)model.StreamFor;
            }
        }
        protected override StreamNo Inverter(StreamFactory model)
        {
            if (model != null)
            {
                return new StreamNo() { 
                    Id=model.Id,
                    No=model.No,
                    RelationId=model.RelationId,
                    Seed=model.Seed,
                    StreamFor=(int)model.StreamFor
                };
            }
            return null;
        }
        public StreamFactory Set(StreamNo dbModel)
        {
            Covariant(dbModel);
            return this;
        }
        /// <summary>
        /// 构建稿件编号 {期刊缩写}-yyMM-{流水号}
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="type"></param>
        /// <param name="relationId"></param>
        /// <returns></returns>
        public string BuildArticleStreamNo(string prefix, BuildStreamType type, int journalId)
        {
            string stream = GetStreamNo(DateTime.Now.ToString("yyMM"), type, journalId);
            stream = stream.PadLeft(4, '0');
            stream = string.Format("{0}-{1}-{2}", prefix, DateTime.Now.ToString("yyMM"), stream);
            return stream;
        }
        /// <summary>
        /// 构建期刊附件编号 {稿件code}-{序号}
        /// </summary>
        /// <param name="articleCode"></param>
        /// <param name="relationId"></param>
        /// <returns></returns>
        public string BuildArticleAppendixCodeStreamNo(string articleCode, AttachmentStatus attachmentStatus, int articleId)
        {
            string stream = GetStreamNo(articleCode, BuildStreamType.Manuscript_Attachment, articleId);
            stream = (int)attachmentStatus + stream.PadLeft(2, '0');
            stream = string.Format("{0}-{1}", articleCode, stream);
            return stream;
        }
        /// <summary>
        /// 构建审稿单编号 {稿件附件Code}-examineSheet-{序号}
        /// </summary>
        /// <returns></returns>
        public string BuildArticleExamineSheetStream(string appendixCode,int appendixId)
        {
            string stream = GetStreamNo(appendixCode, BuildStreamType.Manuscript_Attachment_ExamineSheet, appendixId);
            stream = stream.PadLeft(2, '0');
            stream = string.Format("{0}-examineSheet-{1}", appendixCode, stream);
            return stream;
        }
        private string GetStreamNo(string seed, BuildStreamType streamFor, int relationId)
        {
            using (MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["Context"].ConnectionString))
            {
                try
                {
                    string sqlCommand = "SP_BuildNewStreamNo";
                    MySqlCommand com = new MySqlCommand();
                    com.Parameters.Add(new MySqlParameter("p_Seed", seed));
                    com.Parameters.Add(new MySqlParameter("p_StreamFor", (int)streamFor));
                    com.Parameters.Add(new MySqlParameter("p_RelationId", relationId));
                    com.CommandType = CommandType.StoredProcedure;
                    com.Connection = con;
                    com.CommandText = sqlCommand;
                    con.Open();
                    var result = com.ExecuteScalar();
                    string streamNo = result.ToString();
                    return streamNo;
                }
                catch (Exception e)
                {
                    string s = e.ToString();
                }
            }
            return "";
        }
    }
}
